package com.han.jk_mqtt

import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.FutureTask
import java.util.concurrent.RunnableFuture
import java.util.concurrent.TimeUnit


abstract class AsyncTask<T> : RunnableFuture<T>, Callable<T> {

    private val futureTask = object : FutureTask<T>(this) {
        override fun done() {
            super.done()
            this@AsyncTask.done()
            if (!isCancelled && callback != null) {
                callback?.onDone()
            }
        }
    }

    companion object {
        private val sExecutor = Executors.newCachedThreadPool()
    }

    fun executor(): AsyncTask<T> {
        try {
            sExecutor.execute(this)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return this
    }

    override fun run() {
        TODO("Not yet implemented")
    }

    override fun cancel(mayInterruptIfRunning: Boolean): Boolean {
        TODO("Not yet implemented")
    }

    override fun isCancelled(): Boolean {
        TODO("Not yet implemented")
    }

    override fun isDone(): Boolean {
        TODO("Not yet implemented")
    }

    override fun get(): T {
        TODO("Not yet implemented")
    }

    override fun get(timeout: Long, unit: TimeUnit?): T {
        TODO("Not yet implemented")
    }

    protected fun done() {

    }

    private var callback: Callbock? = null

    interface Callbock {
        fun onDone()
    }
}